<?php

namespace App\Kuafu\Shop\Service;

use App\Kuafu\Shop\Model\ShopPosConfigModel;

class ShopPosConfigService
{

    /**
     * 获得收银机配置信息
     * @param int $shopId
     * @return array
     */
    public static function info(int $shopId): array
    {
        $configJson = ShopPosConfigModel::query()
            ->where('shopId', $shopId)
            ->value('config');

        return json_decode(($configJson == null ? '[]' : $configJson), true);
    }

    /**
     * 修改配置信息
     * @param int $shopId
     * @param array $params
     * @return bool
     */
    public static function update(int $shopId, array $params): bool
    {
        $configModel = ShopPosConfigModel::query()
            ->updateOrCreate([
                'shopId' => $shopId,
            ]);

        $configArr         = json_decode(($configModel == null ? '[]' : $configModel->config), true);
        foreach ($params as $field => $value) {
            $configArr[$field] = $value;
        }

        $configModel->config = json_encode($configArr, JSON_UNESCAPED_UNICODE);
        return $configModel->save();
    }
}